package com.simon.jkxj.ui.diet;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import com.simon.jkxj.R;
import android.content.SharedPreferences;

public class QuitSmokingSettingActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quit_smoking_setting);
        final EditText etCount = findViewById(R.id.et_cig_count);
        final EditText etPrice = findViewById(R.id.et_cig_price);
        final EditText etPerPack = findViewById(R.id.et_cig_per_pack);
        final EditText etTar = findViewById(R.id.et_cig_tar);
        // 读取已保存数据
        SharedPreferences prefs = getSharedPreferences("quit_smoking_prefs", MODE_PRIVATE);
        etCount.setText(String.valueOf(prefs.getInt("cig_count", 0)));
        etPrice.setText(String.valueOf(prefs.getFloat("cig_price", 0)));
        etPerPack.setText(String.valueOf(prefs.getInt("cig_per_pack", 20)));
        etTar.setText(String.valueOf(prefs.getInt("cig_tar", 0)));
        Button btnSave = findViewById(R.id.btn_save);
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = 0, perPack = 20, tar = 0;
                float price = 0;
                try { count = Integer.parseInt(etCount.getText().toString()); } catch (Exception ignored) {}
                try { price = Float.parseFloat(etPrice.getText().toString()); } catch (Exception ignored) {}
                try { perPack = Integer.parseInt(etPerPack.getText().toString()); } catch (Exception ignored) {}
                try { tar = Integer.parseInt(etTar.getText().toString()); } catch (Exception ignored) {}
                SharedPreferences.Editor editor = getSharedPreferences("quit_smoking_prefs", MODE_PRIVATE).edit();
                editor.putInt("cig_count", count);
                editor.putFloat("cig_price", price);
                editor.putInt("cig_per_pack", perPack);
                editor.putInt("cig_tar", tar);
                editor.apply();
                finish();
            }
        });
    }
} 